<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.carNumber" placeholder="请输入车牌号" clearable class="selWidth" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button icon="el-icon-delete" type="danger" size="mini" @click="removeAll()"
            v-hasPermi="['admin:car:deleteIds']">批量删除</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)"
            v-hasPermi="['admin:car:save']">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="multipleTable" v-loading="loading" row-key="getRowKeys" @select="handleCheckBox"
        @select-all="handleSelectAll" :data="listData.list" size="mini" @sort-change="handleSortChange"
        :header-cell-style="{ fontWeight: 'bold', background: '#f8f8f9', color: '#515a6e', height: '40px' }" border>
        <el-table-column type="selection" width="50" />
        <el-table-column prop="number" label="序号" width="50" sortable="custom" />
        <el-table-column label="车牌号" prop="carNumber" min-width="120" />
        <el-table-column label="型号" prop="typeText" min-width="120" />
        <el-table-column label="出车情况" prop="situation" min-width="120" />
        <el-table-column label="车辆状态" prop="statusText" min-width="120" />
        <el-table-column label="操作" min-width="130" fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenEdit(1, scope.row)"
              v-hasPermi="['admin:car:info']">编辑</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row)"
              v-hasPermi="['admin:car:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="listPram.page" :page-sizes="constants.page.limit" :layout="constants.page.layout"
        :total="listData.count" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </el-card>
    <el-dialog :visible.sync="editDialogConfig.visible" :title="editDialogConfig.isCreate === 0 ? '创建' : '编辑'"
      destroy-on-close :close-on-click-modal="false" width="700px">
      <edit v-if="editDialogConfig.visible" :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData" :all-category-data="categoryData" @hideEditDialog="hideEditDialog" />
    </el-dialog>

    <!-- <exportElc
        v-if="exportDialogConfig.visible"

        :all-multiple-selection="multipleSelection"
        @hideExportDialog="hideExportDialog"
      /> -->
  </div>
</template>

<script >
import * as carApi from '@/api/car.js'
import * as roleApi from '@/api/role.js'
import edit from './edit'
import { checkPermi } from "@/utils/permission";
import * as selfUtil from "@/utils/ZBKJIutil"; // 权限判断函数
import * as categoryApi from "@/api/categoryApi";
export default {
  // name: "index"
  components: { edit },
  data() {
    return {
      multipleSelection: [],//勾选的人员对象ID
      constants: this.$constants,
      loading: false,
      listData: { list: [] },
      listPram: {
        carNumber: '',
        /*departmentId: null,
        ipAddress: null,
        macAddress: null,
        personId: null,*/
        page: 1,
        limit: this.$constants.page.limit[0]
      },
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      },
      categoryData: {
        typeList: [],
        statusList: []
      },
    }
  },
  mounted() {
    this.handleGetCarList()
    this.initCategoryData()
  },
  methods: {
    handleSortChange(c) {
      const order = c.order;
      if (order == null || order == 'ascending') {
        this.listPram.sortType = 2;
      } else {
        this.listPram.sortType = 1;
      }
      this.handleGetCarList();
    },
    checkPermi,
    handleSearch() {
      this.listPram.page = 1
      this.handleGetCarList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetCarList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetCarList()
    },
    handlerOpenDel(rowData) {
      this.$confirm('确认删除当前数据').then(() => {
        const _pram = { id: rowData.id }
        carApi.typeDel(_pram).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetCarList()
        })
      })
    },
    //勾选/取消
    handleCheckBox(rows, row) {
      var index = this.multipleSelection.indexOf(row.id);
      if (index !== -1) {
        this.multipleSelection.splice(index, 1);
      } else {
        this.multipleSelection.push(row.id);
      }
    },
    //全部勾选/取消
    handleSelectAll(rows) {
      this.multipleSelection = []; // 清空数组
      if (rows.length > 0) {
        rows.forEach((row) => {
          this.multipleSelection.push(row.id);
        });
      } else {
        this.listData.list.forEach((row) => {
          this.multipleSelection = this.multipleSelection.filter(
            (item) => item != row.id
          )
        })
      }
    }
    ,//批量删除
    removeAll() {
      // 将 this.multipleSelection 赋值给变量 selectedIds
      // 弹出确认对话框并发送删除请求
      if(this.multipleSelection.length==0){
        this.$message.warning('请至少选择一条要删除的数据');
        return;
      }
      this.$confirm("确认要删除选中的" + this.multipleSelection.length + "条数据吗?").then(()=>{
        const loading=this.$loading({text:'删除中'});
        carApi.typeDelBatchCar({ids:this.multipleSelection.join(',')}).then(()=>{
          this.$message.success('删除成功');
          this.listPram.page=1;
          this.handleGetCarList();
        }).finally(()=>{
          loading.close();
        })
      })
    }
    ,
    handleSelectionChange(selection) {
      this.multipleSelection = selection;//存储选中的数据
      // console.log(this.multipleSelection);
    },
    initCategoryData() {
      this.loading = true

      categoryApi.categoryCarList().then(data => {
        this.categoryData = data
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleGetCarList() {
      carApi.typeList(this.listPram).then(data => {
        this.listData = data
      })
    },
    handlerOpenEdit(isCreate, editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetCarList()
    },
    resetQuery() {
      this.listPram.carNumber = '';
      this.handleGetCarList()
    }
  }
}
</script>

<style scoped></style>
